<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * author	:	Tran Van Thanh
 * email	:	thanhtran@vietnambiz.com
 * date		:	30/08/2011
 */

require_once 'client'.EXT;

class Contact extends Client {

	function __construct()
	{
        parent::__construct();
        $this->load->library(array("form_validation","Message","pagination"));
		$this->load->helper(array('custom_string','form'));
    }
    
    function index()
    {
        
        //meta tag
	    $this->data['title'] 			= get_title('[TITLE]',"Đặt hàng liên hệ");
	    $this->data['description'] 		= get_description('[DESCRIPTION]',"Đặt hàng liên hệ");
		$this->data['keywords'] 		= get_keywords('[KEYWORDS]',"Đặt hàng liên hệ");	    
		//end meta tag
        if(!isset($_SESSION['captcha_form_request']) || !$_POST)$_SESSION['captcha_form_request']	=	random_string('numeric',6);
        
        if($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            $this->form_validation->set_message('required', '%s');
            $this->form_validation->set_message('valid_email', '%s');
            $this->form_validation->set_rules("txtName","Họ tên","required");            
            $this->form_validation->set_rules("code","Mã xác nhận","required");
            $this->form_validation->set_rules("txtAddress","Địa chỉ","required");
            $this->form_validation->set_rules("txtEmail","Email","required|valid_email");
            $this->form_validation->set_rules("txtPhone","Điện thoại","required");        
            $this->form_validation->set_rules("txtContent","Nội dung","required");
            $code = $this->input->post('code');
                                  
            if($this->form_validation->run() === TRUE)
            {            	
            	 $fullname			=	strip_tags($this->input->post('txtName'));                 
            	 $email          	=	strip_tags($this->input->post('txtEmail'));
                 $address			=	strip_tags($this->input->post('txtAddress'));
                 $phone				=	strip_tags($this->input->post('txtPhone'));
                 $code_pro			=	strip_tags($this->input->post('txtCodeProducts'));
                 $content           = 	strip_tags($this->input->post('txtContent'));
                 if($code == $_SESSION['captcha_form_request'])
                 {
                    $_data = array(
                        'fullname'  =>  $fullname,
                        'email'     =>  $email,
                        'addr'      =>  $address,
                        'tel'       =>  $phone,
                        'content'   =>  $content,
                        'created_date'  =>  date('Y-m-d H:i:s')
                    );
                    $this->db->insert('lien_he',$_data);
                    $this->message->add("success",'<li class=success><p>Bạn đã gửi thông tin thành công! Xin cảm ơn!<META HTTP-EQUIV=refresh CONTENT=5;URL="'.base_url().'"></p></li>');
                 }
            }
            $_SESSION['captcha_form_request']	=	random_string('numeric',6);
		}
		$this->data['tpl_file']		=	'contact/index';
		$this->load->view('layout/default',$this->data);
    }
 }